<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}

if($_POST['sub']!=''){

    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	unset($_POST['id']);
	unset($_POST['sub']);
    
    $oplist=$_POST["oplist"];
    $opval=$_POST["opval"];
    $data=array('subject'=>$_POST['subject'],"description"=>$_POST["description"],"myinterval"=>$_POST["myinterval"],"enabled"=>$_POST["enabled"],
    "userid"=>$_SESSION['EZ_Admin']['id'],    
    "username"=>$_SESSION['EZ_Admin']['name'],
    "fromdate"=>$_POST["fromdate"],
    "todate"=>$_POST["todate"]
    
    );
    $title=trim($_POST["subject"]);
    if(trim($title)==""){
            jumpajax('300','投票标题不能为空！','');
    }      
 	if($id==0){
        $data["addtime"]=time();
	    $id=$ezhand->insert("vote_subject",$data);
		
	}
	else{
	    $ezhand->update("vote_subject",$data,'subjectid="'.$id.'"',1);
	
	}
    //$ezhand->delete('vote_option','subjectid="'.$id.'"');
    if(!empty($oplist)){
        while(list($k,$v)=each($oplist)){
            $v=trim($v);
            if(!empty($v)){
            $d=array("subjectid"=>$id,"option"=>$v);
            if(!empty($opval[$k])){
                $opid=$ezhand->update("vote_option",$d,"optionid=".$opval[$k]);
            }else{
                $opid=$ezhand->insert("vote_option",$d);    
            }
            
            }
        }
        
    }
    
    jumpajax('200','调查投票项目保存成功','','closeCurrent');
}
else{
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
	
    if($id==0){
	    $row=array();
	}
	else{
	    $row=$ezhand->select("vote_subject",'*','subjectid="'.$id.'"');
	}
}
?>